<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initRegistry()
    {
        Zend_Registry::set('general', $this->getOption('general'));
    }

    protected function _initAutoload()
    {
        // Custom base classes
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Frontend_');
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Core_');
    }

    protected function _initSession()
    {
        $this->bootstrap('db');
        $sessionManager = new Core_Session_Manager();
        Zend_Session::setOptions(array(
            'gc_probability' => 1,
            'gc_divisor'     => 5000
        ));
        Zend_Session::setSaveHandler($sessionManager);

        $general_settings = $this->getOption('general');
        if (!empty($general_settings['sessionNamespace']))
        {
            $frontNamespace = new Zend_Session_Namespace($general_settings['sessionNamespace']);
            Zend_Registry::set('session', $frontNamespace);
        }
    }

    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    protected function _initLocale()
    {

        $this->bootstrap('db');
        Application_Model_Language::determineCurrent();

        $language = Application_Model_Language::getCurrent();

        // Try to get current language from session
        $general_settings = $this->getOption('general');
        if (!empty($general_settings['sessionNamespace']))
        {
            $frontNamespace = new Zend_Session_Namespace($general_settings['sessionNamespace']);
            if ($frontNamespace->lang)
            {
                $languages_list = Application_Model_Language::getList();
                foreach ($languages_list as $lang)
                {
                    if ($lang->getKey() == $frontNamespace->lang)
                    {
                        $language = $lang;
                        break;
                    }
                }
            }
        }

        if ($language)
        {

            // Put current lang object to registry
            Zend_Registry::set('language', $language);
            Application_Model_Language::setCurrent($language->getKey());

            // Try to write value to session
            if (!empty($general_settings['sessionNamespace']))
            {
                $frontNamespace = new Zend_Session_Namespace($general_settings['sessionNamespace']);
                if ($frontNamespace)
                {
                    $frontNamespace->lang = $language->getKey();
                    Zend_Registry::set('session', $frontNamespace);
                }
            }
        }

    }

    protected function _initDatabase()
    {
        $resource = $this->getPluginResource('db');
        $db       = $resource->getDbAdapter();
        if ($db)
        {
            Zend_Registry::set('db', $db);
        }
    }

    protected function _initNavigation()
    {

        // Initialize routing
        $this->bootstrap('FrontController');
        $front  = $this->getResource('FrontController');
        $router = $front->getRouter();

        // Route #1
        $route  = new Zend_Controller_Router_Route(
            'contact',
            array(
                'controller' => 'index',
                'action'     => 'contact'
            )
        );
        $router->addRoute('contact', $route);

        // Route #2
        $route  = new Zend_Controller_Router_Route(
            'home',
            array(
                'controller' => 'index',
                'action'     => 'index'
            )
        );
        $router->addRoute('index', $route);

        // Route #3
        $route = new Zend_Controller_Router_Route_Regex(
            '(.+)\.aspx',
            array(
                'controller' => 'index',
                'action'     => 'page'
            ),
            array(
                1 => 'key'
            ),
            '%s.aspx'
        );
        $router->addRoute('page', $route);

        // Assign initialized router
        $front->setRouter($router); 

        // Build navigation
        $this->bootstrap('view');
        $view  = $this->getResource('view');
        $pages = array(
            array(
                'label' => 'Home',
                'title' => 'Home',
                'uri'   => $view->url(array(), 'index'),
                'order' => -100,
            ),
            array(
                'label' => 'Contact',
                'title' => 'Contact',
                'uri'   => $view->url(array(), 'contact'),
                'order' => 10,
            ),
            array(
                'label' => 'About',
                'title' => 'About',
                'uri'   => $view->url(array('key' => 'about'), 'page'),
                'order' => 20,
            ),
        );

        if (Application_Model_Front_User::isLoggedIn())
        {

            $pages[] = array(
                'label' => 'My Account',
                'title' => 'My Account',
                'uri'   => '/account/my',
                'order' => 30,
            );
            $pages[] = array(
                'label' => 'Logout',
                'title' => 'Logout',
                'uri'   => '/account/logout',
                'order' => 40,
            );

        } else {

            $pages[] = array(
                'label' => 'Create account',
                'title' => 'Create account',
                'uri'   => '/account/create',
                'order' => 30,
            );
            $pages[] = array(
                'label' => 'Log in',
                'title' => 'Log in',
                'uri'   => '/account/login',
                'order' => 40,
            );

        }

        $container = new Zend_Navigation($pages);
        $view->getHelper('navigation')->setContainer($container);
    }

    protected function _initHelpers()
    {
        // Initialize view helpers path
        $view = $this->getResource('view');
        $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Standart_Helper');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/ZendX/JQuery/View/Helper'), 'ZendX_JQuery_View_Helper');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/Frontend/Helper/View'), 'Frontend_Helper_View');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/Frontend/Helper/Widget'), 'Frontend_Helper_Widget');

    }
}
